package cn.wildfire.chat.kit.conversation.message.viewholder;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.os.Build;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.wildfire.chat.kit.WfcUIKit;
import cn.wildfire.chat.kit.annotation.EnableContextMenu;
import cn.wildfire.chat.kit.annotation.MessageContentType;
import cn.wildfire.chat.kit.annotation.ReceiveLayoutRes;
import cn.wildfire.chat.kit.annotation.SendLayoutRes;
import cn.wildfire.chat.kit.conversation.message.model.UiMessage;
import cn.wildfirechat.message.CallStartMessageContent;

@MessageContentType(CallStartMessageContent.class)
@ReceiveLayoutRes(resId = R2.layout.conversation_item_voip_receive)
@SendLayoutRes(resId = R2.layout.conversation_item_voip_send)
@EnableContextMenu
public class VoipMessageViewHolder extends NormalMessageContentViewHolder {

    @BindView(R2.id.contentTextView)
    TextView textView;

    private FragmentActivity activity;

    public VoipMessageViewHolder(FragmentActivity activity, RecyclerView.Adapter adapter, View itemView) {
        super(activity, adapter, itemView);
        this.activity = activity;
        ButterKnife.bind(this, itemView);
    }

    @SuppressLint("DefaultLocale")
    @Override
    public void onBind(UiMessage message) {
        CallStartMessageContent content = (CallStartMessageContent) message.message.content;
        if (content.getStatus() == 0) {
            textView.setText("对方未接听");
        } else if (content.getStatus() == 1) {
            textView.setText("通话中");
        } else {
            String text;
            if (content.getConnectTime() > 0) {
                long duration = (content.getEndTime() - content.getConnectTime()) / 1000;
                if (duration > 3600) {
                    text = String.format("通话时长 %d:%02d:%02d", duration / 3600, (duration % 3600) / 60, (duration % 60));
                } else {
                    text = String.format("通话时长 %02d:%02d", duration / 60, (duration % 60));
                }
                /**
                 * modify by HEKE , 2019/8/22 15:08
                 * TODO  修复语音与视频通话消息类型区分
                 */
                if (((CallStartMessageContent) message.message.content).isAudioOnly()) {
                    text = "语音" + text;
                } else {
                    text = "视频" + text;
                }
            } else {
                text = "对方未接听";
            }
            textView.setText(text);
        }
        /**
         * modify by HEKE , 2019/9/2 11:50 TODO 当用户不在通话界面时，添加点击文本返回通话界面功能
         */
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                call(view);
            }
        });
    }

    @OnClick(R2.id.contentTextView)
    public void call(View view) {
        try {
            if (((CallStartMessageContent) message.message.content).getStatus() == 1) {
                return;
            }
            /**
             * modify by HEKE , 2019/8/26 0:43
             * TODO 权限处理
             */
            boolean isAudioOnly = ((CallStartMessageContent) message.message.content).isAudioOnly();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (isAudioOnly) {
                    activity.requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO}, 100);
                    boolean granted = activity.checkSelfPermission(Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
                    if (!granted) {
                        Toast.makeText(activity, "请允许打开语音权限!", Toast.LENGTH_SHORT).show();
                        return;
                    }
                } else {
                    String[] permissions = new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA};
                    activity.requestPermissions(permissions, 100);
                    for (String permission : permissions) {
                        boolean granted = activity.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
                        if (!granted) {
                            String msg = permission.equals(Manifest.permission.RECORD_AUDIO) ? "请允许打开语音权限!" : "请允许打开摄像头权限!";
                            Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
                }
            }
            /**
             * modify by HEKE , 2019/8/22 15:09
             * TODO 重新点击语音或者视频通话时，获取是否仅仅只是语音通话类型
             */
            WfcUIKit.onCall(context, message.message.conversation.target, true, isAudioOnly);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
